var async = require('async'),
  user = require('../../app/model/user'),
  org = require('../../app/model/organization'),
  userRole = require('../../app/model/userRole');

exports.pass = function(req, res, next, connection) {
  var uid = req.user.id;
  var area;
  var departmentId = [];
  var rule = [];
  // user.findEmployeeMsgByUid(connection,uid,function(data){
  // 	console.log('data-workplace:',data);
  //    if(data != null && data.length != 0) {
  //      workplace=data[0].workplace;
  //    }else{
  //      workplace = "";
  //    }

  // });
  org.selOrgAddress(connection, uid, function(data) {
    console.log('用户所在区域:', data);
    area = data[0];
  });
  org.selOrgId(connection, uid, function(data) {
    console.log('用户负责部门:', data);
    departmentId = data;
  });
  userRole.findRoleNameByUid(connection, uid, function(data) {
    // console.log('用户角色:',data);
    var flag = 0;
    for (var i = 0; i < data.length; i++) {
      if (data[i].roleName == "admin") {
        flag = 3;
      } else if (data[i].roleName == "绩效管理员" || data[i].roleName == "高层领导") {
        flag > 2 ? null : flag = 2;
      } else if (data[i].roleName == "部门经理") {
        flag > 1 ? null : flag = 1;
      }
    }
    // console.log('flag:',flag);
    switch (flag) {
      case 1:
        rule.push(['departmentId', departmentId]);
        break;
      case 2:
        rule.push(['area', area]);
        break;
    }
    // res.cookie('rule',rule);
    req.session.rule = rule;
    console.log('rules:', rule);
    next();
  });
};